package com.mychart.controller;

import com.mychart.entity.domain.HttpRes;
import com.mychart.entity.dto.QueryUserDTO;
import com.mychart.entity.dto.UserDTO;
import com.mychart.entity.dto.UserFriendDTO;
import com.mychart.entity.mongo.ChartRecord;
import com.mychart.entity.po.ChartUserPO;
import com.mychart.service.IChartUserService;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author czh
 * @since 2023-10-16
 */
@RestController
@RequestMapping("/chartUser")
public class ChartUserController {

    @Autowired
    IChartUserService chartUserService;
//    chartUser/queryUsersByName
    @PostMapping("queryUsersByName")
    public HttpRes findUsersByName(@RequestBody QueryUserDTO queryUserDTO) {
        List<ChartUserPO> list = chartUserService.findUsersByName(queryUserDTO.getUserName());
        HttpRes success = HttpRes.success();
        success.addParam("users",list);
        return success;
    }

    @PostMapping("addFriend")
    public HttpRes addFriend(@RequestBody UserFriendDTO userFriendDTO){
        chartUserService.addFriend(userFriendDTO);
        return HttpRes.success();
    }

    @PostMapping("confirmNewFriendApply")
    public HttpRes confirmNewFriendApply(@RequestBody UserFriendDTO userFriendDTO){
        chartUserService.confirmNewFriendApply(userFriendDTO);
        return HttpRes.success();
    }

    @GetMapping("getFriendApplyList")
    public HttpRes getFriendApplyList(@RequestParam String userName) {
        List<UserDTO> list = chartUserService.getFriendApplyList(userName);
        HttpRes success = HttpRes.success();
        success.addParam("users",list);
        return success;
    }

    @GetMapping("getAllFriends")
    public HttpRes getAllFriends(String userName) {
        List<UserDTO> list = chartUserService.getFriends(userName);
        HttpRes success = HttpRes.success();
        success.addParam("friends",list);
        return success;
    }

    @GetMapping("getChartRecord/{from}/{to}")
    public HttpRes getChartRecord (@PathVariable("from") String from,@PathVariable("to") String to) {
        List<ChartRecord> chartRecords =  chartUserService.getChartRecord(new UserFriendDTO(from,to));
        HttpRes success = HttpRes.success();
        success.addParam("records",chartRecords);
        return success;
    }
}
